<?php 
abstract class AdminMaster extends sfAction{
	public $data   = null;
	public $formData = array();
	public $formFilter = array(
				'filter_key' 		=> '',
				'filter_enabled' 	=> '-1',
				'filter_submit'		=> ''
			);
	public function preExecute(){
		sfContext::getInstance()->getConfiguration()->loadHelpers('I18N');
		
		$account = $this->getUser()->getAttribute('admin.accounts',array());
		SysAccounts::updateLoggedTime($account['id']);
		
		if(!is_array($account) ||  count($account)!=3 || empty($account['login_user']) || intval($account['id'])<=0 || intval($account['admin_type'])<0 ||intval($account['admin_type'])>2 ){
			$this->redirect( sfConfig::get('app_prefix') . $this->generateUrl('home_login'));
		}
		
		
		/*checking permission module*/
		$isPermission = false;
		
		$menuJson 	= json_decode(file_get_contents(dirname(__FILE__).'/../config/admin.json'),true);
		foreach ($menuJson as $module){
			foreach($module['actions'] as $key=>$action){
				if($action['name'] == '/'.$this->getModuleName().'/'.$this->getActionName()){
					$isPermission = SysAccounts::checkPermission($action);
				}
			}
		}
		if(!$isPermission){
			foreach ($menuJson as $module){
				if($module['name']=='allowAction'){
					$isPermission = SysAccounts::checkAjaxPermission($module, $this->getModuleName().'/'.$this->getActionName());
				}
			}
		}
		if(!$isPermission && $this->getModuleName() !='preview' && $this->getModuleName() !='user' && $this->getModuleName() !='uploadControl'){
			$this->redirect( sfConfig::get('app_prefix') . $this->generateUrl('home_login'));
		}
		/*End */
		
		
		
		
		$request = $this->getRequest();
		$this->data['filter'] 		= $request->getParameter('filter');
		if(empty($this->data['filter']) && !is_array($this->data['filter'])){		//if data is empty (or not filter post), get data from url
			foreach($this->formFilter as $key => $val){
				$this->data['filter'][$key] = $request->getParameter($key, $val);
			}
		}
		
		$this->listFilter(); 
		
		$this->data['action'] 			= $request->getParameter('action_name', '');		//get action
		$this->data['is_post']			= $request->isMethod('POST');					//checking post
		$this->data['select_list']	= $request->getParameter('select_list', array());		//get array of id selected
		$this->data['id']						= $request->getParameter('id', -1);				//get id for edit data
		$this->data['app_prefix']	= sfConfig::get('app_prefix');
		$this->data['redirect'] 	= sfConfig::get('app_prefix').'/'.$this->getModuleName().'/'.$this->getActionName();
		$this->data['ids']				= $request->getParameter('ids',array());
		$this->data['sort_orders']	= $request->getParameter('sort_orders',array());
		$this->data['ajax_value']		= $request->getParameter('val','');
		$this->data['is_alert']			= 0;
		
		//ONLY USE FOR MULTILANGUAGES
		$this->data['langs']		= SysLanguages::getAllLang();
		//END
		$this->data['pagination']	= array(
					'page'		=> $request->getParameter('page', 1),
					'limit'		=> $request->getParameter('limit', sfConfig::get('app_pagination_limit')),
					'page_show'	=> sfConfig::get('app_pagination_page_show')
				);
		if(is_array($this->data['filter']) && isset($this->data['filter']['filter_submit']) && !empty($this->data['filter']['filter_submit'])){
			$this->data['pagination']['page'] = 1;
			unset($this->data['filter']['filter_submit']);
		}
		$this->data['filter_action']	= $this->generalAction();
		$this->data['navigation']	= __('module_'.$this->getModuleName()) . ' › '. __('module_'.$this->getModuleName().'_action_'.$this->getActionName());
		
		
	}
	public function listFilter(){
		
	}
	public function execute($request){
		$this->formData				= $request->getParameter('AdminFForm', array());
		
		switch ($this->data['action']){
			case ADMIN_DEFINED::DELETE_RECORD:
				$this->delRecord();
				$this->filterList();
				break;
			case ADMIN_DEFINED::DISABLE:
				$this->disableRecord();
				$this->filterList();
				break;
			case ADMIN_DEFINED::ENABLE:
				$this->enableRecord();
				$this->filterList();
				break;
			case ADMIN_DEFINED::ADD_RECORD:
				$this->addRecord();
				break;
			case ADMIN_DEFINED::EDIT_RECORD:
				$this->editRecord();
				break;
			case ADMIN_DEFINED::SAVE_ORDER:
				$this->saveOrder();
				$this->filterList();
				break;
			case ADMIN_DEFINED::SET_HOME:
				$this->setAttribute();
				break;
			default:
				$this->filterList();
				break;
		}
		
		$this->setVar('data', $this->data);
	}
	
	#ATTRIBUTE
	public function setAttribute(){
		
	}
	
	#DELETE
	public function delRecord(){
		
	}
	
	#DISABLE
	public function disableRecord(){
		
	}
	#ENABLE
	public function enableRecord(){
	
	}
	#ADD
	private function addRecord(){
		$this->data['navigation'].=' › '. __('nav_add_new');
		$this->data['button_action'] = __('nav_add_new');
		$this->addnew();
	}
	public function addnew(){
		
	}
	#EDIT
	private function editRecord(){
		$this->data['navigation'].=' › '. __('nav_edit');
		$this->data['action'].='&id='.$this->data['id'];
		$this->data['button_action'] = __('nav_edit');
		$this->edit();
	}
	public function edit(){
		
	}
	#SAVE
	public function saveOrder(){
	
	}
	#LIST
	private function filterList(){
		$this->data['navigation'].=' › '. __('nav_list');
		
		$this->showList();
		
		$pag = new PaginationHelper2($this->data['pagination']);
		$this->data['pagination_pager']	= $pag->getPaginations();
		if($this->data['filter']['filter_key'] == '*'){
			$this->data['filter']['filter_key'] = '';
		}
	}
	public function showList(){
		
	}
	#ACTION
	private function generalAction(){
		$action = '?';
		if(is_array($this->data['filter'])){
			foreach($this->data['filter'] as $key => $val){
				if(!empty($val)){
					$action.='&'.$key.'='.urlencode($val);
				}else{
					$action.='&'.$key.'=';
				}
				
			}
		}
		return $action;
	}
}